<template>
  <StyleProvider hashPriority="low" :transformers="[legacyLogicalPropertiesTransformer]">
    <ConfigProvider :locale="getAntdLocale">
      <AppProvider>
        <RouterView />
      </AppProvider>
    </ConfigProvider>
  </StyleProvider>
</template>

<script lang="ts" setup>
  import { onMounted, CSSProperties, ref, onUnmounted } from "vue"
  import { StyleProvider, legacyLogicalPropertiesTransformer, ConfigProvider } from "ant-design-vue"
  import { AppProvider } from "/@/components/Application"
  import { useLocale } from "/@/locales/useLocale"
  import { isLoginPage, PageEnum } from "/@/enums/pageEnum"
  import * as authCache from "/@/utils/cache/module"
  import { useAppStore } from "/@/store/modules/app"
  import { useUserStore } from "/@/store/modules/user"
  const userStore = useUserStore()

  // 解决日期时间国际化问题
  import "dayjs/locale/zh-cn"
  const { getAntdLocale } = useLocale()
  onMounted(async () => {
    const tenantName = authCache.getSelectTenant()?.name
    if (tenantName) {
      window.document.title = window.document.title + "-" + tenantName
    }

    document.addEventListener("visibilitychange", handleVisibilitychange, true)
    window.addEventListener('beforeunload', handleBeforeUnload)

    await userStore.getUserInfoAction()
  })

  onUnmounted(() => {
    document.removeEventListener("visibilitychange", handleVisibilitychange, true)
    window.removeEventListener('beforeunload', handleBeforeUnload);
  })

  const handleBeforeUnload = (event) => {
    const useApp = useAppStore()
    const pageDataSaveFlag = useApp.getPageDataSaveFlag
    if (pageDataSaveFlag) {
      return
    }
    // 显示确认对话框
    event.preventDefault();
    // 为了兼容处理，Chrome需要设置returnValue
    event.returnValue = '确定离开？';
    return "你确定离开？"
  }

  const handleVisibilitychange = () => {
    const isHidden = document.hidden
    if (isHidden) {
      // 页面切入,
      if (isLoginPage()) {
        return
      }
    } else {
      // 页面切出,浏览器不处于当前页面
    }
  }
</script>

<style lang="less">
  body {
    // line-height: 1.5715;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji",
      "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
    overflow: auto;
    overflow-y: auto !important;
  }
</style>
